package com.znz.web;

import com.znz.domain.Part;
import com.znz.domain.Project;
import com.znz.domain.Tcustomer;
import com.znz.service.CommonService;
import com.znz.service.CustomerSerService;
import com.znz.util.common.Page;
import com.znz.util.common.ReturnObj;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created with IntelliJ IDEA.
 * User: duyuxin
 * Date: 15-4-30
 * Time: 下午8:55
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/customerService")
public class CustomerSerController extends AbstractController {
    @Autowired
    private CustomerSerService customerSerService;
    @Autowired
    private CommonService commonService;


    private Logger logger = LoggerFactory.getLogger(CustomerSerController.class);


    @RequestMapping(value={"/","/customerService"})
    public ModelAndView userList(String userName,Page page) {
        try{
            ModelAndView modelAndView=null;
            Map<String,Object> map=new HashMap<String, Object>();
            Page<Tcustomer> tcustomerPage=customerSerService.getCustomerServiceList(userName, page);
            map.put("tcustomerPage",tcustomerPage);
            modelAndView = getTemplate("customerService/list",map);
            return modelAndView;
        }catch (Exception e){
            logger.error("页面初始化异常", e);
            ModelAndView modelAndView = getTemplate("uncaughtException", null);
            return modelAndView;
        }
    }
    @RequestMapping(value="toAdd")
    private ModelAndView toAdd(String user) {
        try {
            Map<String,Object> map=new HashMap<String,Object>();
            List<Part> partList=commonService.getPartAll();
            map.put("partList",partList);
            if(null!=partList && partList.size()>0){
                List<Project> projectList=commonService.getProjectListByPart(partList.get(0).getId());
                map.put("projectList",projectList);
            }
            //TODO 需要获取项目列表放到map里，用于配置项目列表
            ModelAndView modelAndView = getTemplate("customerService/add",map);
            return modelAndView;
        } catch (Exception e) {
            logger.error("新增客服页面初始化异常",e);
            ModelAndView modelAndView = getTemplate("uncaughtException",null);
            return  modelAndView;
        }
    }
    @RequestMapping(value="toEdit")
    private ModelAndView toEdit(Long   customer_id) {
        try {

            //TODO 这里需要根据客服id获取客服对象放到map里

            Map<String,Object> map=new HashMap<String,Object>();

            //TODO 同时还需要获取项目列表放到map里，用于配置项目列表
            ModelAndView modelAndView = getTemplate("customerService/edit",map);

            return modelAndView;
        }catch (Exception e){
            logger.error("修改客服页面初始化异常",e);
            ModelAndView modelAndView = getTemplate("uncaughtException",null);
            return  modelAndView;
        }
    }

    @RequestMapping(value = "getCustomerServiceList")
    @ResponseBody
    public ReturnObj getCustomerServiceList(@RequestParam String userName,Page page){
         ReturnObj returnObj=null;
         //TODO 查看所有已经注册的客服账户列表
        try {
            Page<Tcustomer> tcustomerPage=customerSerService.getCustomerServiceList(userName,page);
            returnObj=ReturnObj.createSuccess(tcustomerPage);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            returnObj=ReturnObj.createError();
        }
        return  returnObj;
    }

    @RequestMapping(value = "getProjectListByPartId")
    @ResponseBody
    public ReturnObj getProjectListByPartId(@RequestParam Long partId){
        ReturnObj returnObj=null;
        //TODO 根据部位查询项目
        try {
            List<Project> projectList=customerSerService.getProjectListByPartId(partId);
            returnObj=ReturnObj.createSuccess(projectList);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            returnObj=ReturnObj.createError();
        }
        return returnObj;
    }

    @RequestMapping(value = "saveCustomerService")
    @ResponseBody
    public ReturnObj saveCustomerService(@RequestBody String customerServiceVo){
         ReturnObj returnObj=null;
        //TODO 保存客服信息
        try {
            JSONObject customerService= JSONObject.fromObject(customerServiceVo);
            Boolean flag=customerSerService.saveCustomerService(customerService);
            returnObj=ReturnObj.createSuccess(flag);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            returnObj=ReturnObj.createError();
        }
        return  returnObj;
    }
    @RequestMapping(value = "delCustomerService")
    @ResponseBody
    public ReturnObj  delCustomerService(Long cId){
        ReturnObj returnObj=null;
        //TODO 删除客服信息(修改客服状态)
        try {
            boolean flag=customerSerService.delCustomerService(cId);
            returnObj=ReturnObj.createSuccess(flag);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            returnObj=ReturnObj.createError();
        }
        return returnObj;
    }
}
